package me.buick.util.jmeter.snmpprocess.snmpmgmt;

import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SERVER_TYPE_LINUX;

import me.buick.util.snmp.core.SupportedOS;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * 
 * This class is used to do some pre-process between SNMPProcessSampler & SNMPDataAdaptor
 * 
 * @author buick
 *
 */

public class SNMPProcessSampleResultProcessor {
	
	static final Logger log 	       = LoggingManager.getLoggerForClass();
	
	private String server 		 	   = "127.0.0.1";
	private int port   		     	   = 161;
	private String snmpSuiteID   	   = "";
	private String serverType          = SERVER_TYPE_LINUX;
	private int snmpDataRetriveInterval = 1500; 
	
	private SNMPProcessDataAdaptor     adaptor;
	
	public SNMPProcessSampleResultProcessor(String server, String port, String snmpSuiteID, String serverType,String snmpDataRetriveInterval){
		
		this.server        = server;
		this.serverType    = serverType;
		this.snmpSuiteID   = snmpSuiteID;
		try{
			this.port = Integer.parseInt(port);
			this.snmpDataRetriveInterval = Integer.parseInt(snmpDataRetriveInterval);
		}catch(Exception e){
			log.error(e.getLocalizedMessage(), e);
		}

	}
	
	public void readSampleResult(SNMPProcessSampleResult rs) throws Exception{
		
		rs.setServer(this.server);
		rs.setPort(this.port+"");
		rs.setServerType(this.serverType);
		rs.setSnmpSuiteID(this.snmpSuiteID);
		
		if(adaptor == null)
			adaptor = new SNMPProcessDataAdaptor(this.server, this.port, this.string2OS(serverType),this.snmpDataRetriveInterval);
		adaptor.fillSampleResult(rs);
	}
	
	private SupportedOS string2OS(String serverType){
		if(serverType !=null && serverType.toLowerCase().contains("windows")){
			return SupportedOS.Windows;
		}else{
			return SupportedOS.Linux;
		}
	}
	
	public SNMPProcessDataAdaptor getAdaptor(){
		return this.adaptor;
	}
	
}
